package com.djl.zenithbite.service.comment.impl;

import com.djl.zenithbite.common.response.BaseResponse;
import com.djl.zenithbite.common.threadlocal.UserContextHolder;
import com.djl.zenithbite.dto.request.comment.AddCommentRequest;
import com.djl.zenithbite.dto.request.comment.DeleteCommentRequest;
import com.djl.zenithbite.dto.response.comment.CommentResponse;
import com.djl.zenithbite.entity.*;
import com.djl.zenithbite.repository.*;
import com.djl.zenithbite.service.comment.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private UserReviewMapper userReviewMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public BaseResponse list() {
        UserReviewExample userReviewExample = new UserReviewExample();
        userReviewExample.setOrderByClause("created_at desc");
        UserReviewExample.Criteria criteria = userReviewExample.createCriteria();
        criteria.andUserIdEqualTo(UserContextHolder.getUserPrimaryId());
        List<UserReviewWithBLOBs> userReviews = userReviewMapper.selectByExampleWithBLOBs(userReviewExample);

        BaseResponse<List<CommentResponse>> response = new BaseResponse<>();
        response.setCode(BaseResponse.SUCCESS);
        response.setData(userReviews.stream().map(this::convertComment).collect(Collectors.toList()));
        return response;
    }

   private CommentResponse convertComment(UserReviewWithBLOBs userReview){
        Product product = productMapper.selectByPrimaryKey(userReview.getProductId());
        Store store = storeMapper.selectByPrimaryKey(userReview.getStoreId());
        Order order = orderMapper.selectByPrimaryKey(userReview.getOrderId());
        User user = userMapper.selectByPrimaryKey(userReview.getUserId());

       CommentResponse response = new CommentResponse();
       response.setUserReview(userReview);
       response.setProduct(product);
       response.setOrder(order);
       response.setStore(store);
       response.setUser(user);

       return response;

    }

    @Override
    public BaseResponse storeList(Integer storeId) {
        UserReviewExample example = new UserReviewExample();
        example.setOrderByClause("created_at desc");
        UserReviewExample.Criteria criteria = example.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        List<UserReviewWithBLOBs> userReviews = userReviewMapper.selectByExampleWithBLOBs(example);

        BaseResponse<List<CommentResponse>> response = new BaseResponse<>();
        response.setCode(BaseResponse.SUCCESS);
        response.setData(userReviews.stream().map(this::convertComment).collect(Collectors.toList()));
        return response;
    }

    @Override
    public BaseResponse productList(Integer productId) {
        UserReviewExample example = new UserReviewExample();
        example.setOrderByClause("created_at desc");
        UserReviewExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(productId);
        List<UserReviewWithBLOBs> userReviews = userReviewMapper.selectByExampleWithBLOBs(example);

        BaseResponse<List<CommentResponse>> response = new BaseResponse<>();
        response.setCode(BaseResponse.SUCCESS);
        response.setData(userReviews.stream().map(this::convertComment).collect(Collectors.toList()));
        return response;
    }

    @Override
    public BaseResponse add(AddCommentRequest request) {
        UserReviewWithBLOBs userReviewWithBLOBs = generateUserReview(request);
        userReviewMapper.insertSelective(userReviewWithBLOBs);

        BaseResponse<Void> response = new BaseResponse<>();
        response.setCode(BaseResponse.SUCCESS);
        return response;
    }

    @Override
    public BaseResponse delete(DeleteCommentRequest request) {
        userReviewMapper.deleteByPrimaryKey(request.getReviewId());

        BaseResponse<Void> response = new BaseResponse<>();
        response.setCode(BaseResponse.SUCCESS);
        return response;
    }

    public UserReviewWithBLOBs generateUserReview(AddCommentRequest request){
        UserReviewWithBLOBs userReviewWithBLOBs = new UserReviewWithBLOBs();
        userReviewWithBLOBs.setComment(request.getComment());
        userReviewWithBLOBs.setReviewImages(request.getReviewImages());
        userReviewWithBLOBs.setUserId(UserContextHolder.getUserPrimaryId());
        userReviewWithBLOBs.setOrderId(request.getOrderId());
        userReviewWithBLOBs.setProductId(request.getProductId());
        userReviewWithBLOBs.setStoreId(request.getStoreId());
        userReviewWithBLOBs.setRating(request.getRating());
        userReviewWithBLOBs.setReviewStatus(true);
        userReviewWithBLOBs.setCreatedAt(new Date());
        userReviewWithBLOBs.setUpdatedAt(new Date());
        return userReviewWithBLOBs;
    }
}
